package com.lst.servlet;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.InformListReqPara;
import com.lst.entity.response.InformListResPara;
import com.lst.model.MstClient;
import com.lst.model.Plan;
import com.lst.service.MstClientService;
import com.lst.service.PlanService;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: InformListServlet
 * @Description: 通知管理接口
 * @author zmm
 * @date 2017年10月16日 
 * 
 */
public class InformListServlet extends BaseServlet {

    private static final long serialVersionUID = 1L;

    private MstClientService mstClientService = ctx.getBean(MstClientService.class);
    
	private PlanService planService = ctx.getBean(PlanService.class);

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
	doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	InformListResPara resPara = new InformListResPara();

	try {
		InformListReqPara reqPara = getReqPara(request);

	    if (isCheckField(reqPara, resPara)) {
		Integer userid = Integer.valueOf(reqPara.getUserid());
	
		MstClient mstClient=mstClientService.selectByPrimaryKey(Integer.valueOf(userid));
		if(mstClient!=null){
			resPara.setDiscussStatus(mstClient.getDiscussStatus());
			resPara.setThumbStatus(mstClient.getThumbStatus());
			resPara.setSystemStatus(mstClient.getSystemStatus());
		}else{
			resPara.setDiscussStatus(true);
			resPara.setThumbStatus(true);
			resPara.setSystemStatus(true);
		}
		
		Plan plan=planService.selectByUser(userid);
		if(plan!=null){
			resPara.setNoticeStatus(plan.getIsNotice());
			resPara.setNoticeTime(plan.getNoticeTime());
		}else{
			resPara.setNoticeStatus(false);
			resPara.setNoticeTime("00:00");
		}

		resPara.setCode(CommCode.M_Y000000);
		resPara.setMessage(CommCode.M_Y000001);
	    }
	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0001);

	    logger.error("doPost error: ", e);
	}

	try {
	    response.setContentType("application/json;charset=utf-8");
	    response.setHeader("pragma", "no-cache");
	    response.setHeader("cache-control", "no-cache");

	    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
		    .excludeFieldsWithoutExposeAnnotation().serializeNulls()
		    .create();
	    String jsonResult = gson.toJson(resPara);
	    logger.info("InformListServlet jsonResult: " + jsonResult);

	    PrintWriter out = response.getWriter();
	    out.print(jsonResult);
	    out.flush();
	    out.close();

//	    logger.info("doPost end run(s): "+ DateUtil.calLastedTime(startDate));
	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0003);

	    logger.error("doPost error: ", e);
	}
    }

    @Override
    public InformListReqPara getReqPara(HttpServletRequest request) {

    	InformListReqPara reqPara = null;

	try {
	    BaseRequest baseRequest = RequestUtils.getRequestPara(request,
		    new InformListReqPara());

	    reqPara = (InformListReqPara) baseRequest.clone();
	    String userid = request.getParameter("userid");

	//    logger.info("reqPara userid: " + userid);
	   
	    reqPara.setUserid(userid);

	} catch (Exception e) {
	    logger.error("getReqPara error: ", e);
	}
	return reqPara;
    }

    @Override
    public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
	boolean flag = false;

	InformListReqPara cmReqPara = (InformListReqPara) reqPara;
	InformListResPara cmResPara = (InformListResPara) resPara;

	if (StringUtils.isEmpty(cmReqPara.getUserid())) {
	    cmResPara.setCode(CommCode.M_A000000);
	    cmResPara.setMessage(CommCode.M_P000002);
	} else {
	    flag = true;
	}
	return flag;
    }
}
